<template>
  <div class="jaguar-view">
    <div class="jaguar-search">
      <el-form :inline="true" :model="searchForm" @submit.prevent>
        <el-form-item label="用户">
          <user-select
            v-model="searchForm.userId"
            placeholder="请输入用户"
            @change="getData(1)"
          />
        </el-form-item>
        <el-form-item>
          <search-button :search="getData" />
          <reset-button :search-form="searchForm" :search="getData" />
        </el-form-item>
      </el-form>
    </div>

    <div class="jaguar-control">
      <add-button @click="handleCreate" />
    </div>

    <el-card class="jaguar-table">
      <el-table :data="tableData" v-loading="loading" table-layout="auto">
        <el-table-column type="index" />
        <el-table-column prop="user" label="用户">
          <template #default="scope">
            {{
              scope.row.userNickName
                ? scope.row.userNickName + '（' + scope.row.userAccount + '）'
                : scope.row.userAccount
            }}
          </template>
        </el-table-column>
        <el-table-column prop="menuName" label="菜单" />
        <el-table-column prop="deptName" label="部门" />
        <el-table-column label="操作">
          <template #default="scope">
            <delete-button
              @confirm="
                ({ instance, done }) =>
                  handleDelete(scope.row, { instance, done })
              "
            />
          </template>
        </el-table-column>
      </el-table>

      <table-page :page="page" :search="getData" />
    </el-card>

    <Edit :dialog="dialog" @success="getData()" />
  </div>
</template>
<script>
import { fetchList, del } from '@/api/upms/userDataPermission'
import Edit from './Edit'

export default {
  name: 'UserDataPermission',
  data() {
    return {
      loading: false,
      tableData: [],
      searchForm: {},
      page: {
        total: 0,
        current: 1,
        size: 10,
      },
      dialog: {
        entity: null,
        operation: null,
        visible: false,
      },
    }
  },
  components: {
    Edit,
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData(current, size) {
      if (current) this.page.current = current
      if (size) this.page.size = size

      this.loading = true
      fetchList(
        Object.assign(
          {
            'orders[0].asc': 'false',
            'orders[0].column': 'id_',
            current: this.page.current,
            size: this.page.size,
          },
          this.searchForm
        )
      )
        .then((res) => {
          this.tableData = res.data.data.records
          this.page.total = res.data.data.total
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleCreate() {
      this.dialog.entity = null
      this.dialog.operation = 1
      this.dialog.visible = true
    },
    handleEdit(row) {
      this.dialog.entity = row
      this.dialog.operation = 2
      this.dialog.visible = true
    },
    handleView(row) {
      this.dialog.entity = row
      this.dialog.operation = 3
      this.dialog.visible = true
    },
    handleDelete(row, { instance, done }) {
      del(row.id)
        .then(() => {
          done()
          this.$message.success('删除成功')
          this.getData()
        })
        .finally(() => {
          instance.confirmButtonLoading = false
        })
    },
  },
}
</script>
